package per.dg.tencent.views.recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import per.dg.tencent.R;

public abstract class BaseSuspensionHeadItemDecoration<T> extends RecyclerView.ItemDecoration {

    private List<T> mList;
    private LayoutInflater mLayoutInflater;

    boolean isFitstShowSuspensionHeadItem = false;

    private ViewGroup.LayoutParams mLayoutParams;
    int measuredHeight;
    int measuredWidth;
    int height;
    int width;
    int paddingLeft;
    int paddingTop;

    public BaseSuspensionHeadItemDecoration(Context mContext, List<T> mList){
        this.mList = mList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int firstPosition = ((GridLayoutManager)parent.getLayoutManager()).findFirstVisibleItemPosition();
        int showSuspensionHeadPosition = getShowSuspensionHeadPosition(firstPosition);
        if(showSuspensionHeadPosition == -1){
            return;
        }
        if(isFitstShowSuspensionHeadItem){
            deawSuspensionHead(showSuspensionHeadPosition, c, parent);
        }else {
            if(firstPosition == showSuspensionHeadPosition){
                isFitstShowSuspensionHeadItem = true;

                View view = parent.findViewHolderForAdapterPosition(showSuspensionHeadPosition).itemView;

                mLayoutParams = view.getLayoutParams();
                measuredWidth = view.getMeasuredWidth();
                measuredHeight = view.getMeasuredHeight();
                height = view.getHeight();
                width = view.getWidth();
                paddingLeft = view.getPaddingLeft();
                paddingTop = view.getPaddingTop();

                if (mLayoutParams.width == ViewGroup.LayoutParams.MATCH_PARENT) {
                    //如果是MATCH_PARENT，则用父控件能分配的最大宽度和EXACTLY构建MeasureSpec。
                    measuredWidth = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.EXACTLY);
                } else if (mLayoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
                    //如果是WRAP_CONTENT，则用父控件能分配的最大宽度和AT_MOST构建MeasureSpec。
                    measuredWidth = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.AT_MOST);
                } else {
                    //否则则是具体的宽度数值，则用这个宽度和EXACTLY构建MeasureSpec。
                    measuredWidth = View.MeasureSpec.makeMeasureSpec(mLayoutParams.width, View.MeasureSpec.EXACTLY);
                }
                //高度同理
                if (mLayoutParams.height == ViewGroup.LayoutParams.MATCH_PARENT) {
                    measuredHeight = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.EXACTLY);
                } else if (mLayoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
                    measuredHeight = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.AT_MOST);
                } else {
                    measuredHeight = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
                }
                view.layout(parent.getPaddingLeft(), 0, parent.getPaddingLeft()+view.getWidth(), view.getHeight());
                deawSuspensionHead(showSuspensionHeadPosition, c, parent);
            }
        }
    }

    private void deawSuspensionHead(int showSuspensionHeadPosition, Canvas c, RecyclerView parent){

        View topTitleView = mLayoutInflater.inflate(getSuspensionHeadViewResource(),null);
        showView(showSuspensionHeadPosition, topTitleView);
        topTitleView.setLayoutParams(mLayoutParams);
        topTitleView.measure(measuredWidth, measuredHeight);
        topTitleView.layout(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getPaddingLeft()+width, parent.getPaddingTop()+height);

        c.translate(parent.getPaddingLeft(), parent.getPaddingTop());
        topTitleView.draw(c);
    }

    private int getShowSuspensionHeadPosition(int firstVisibleItemPosition){
        if(mList == null || mList.size() == 0){
            return -1;
        }
        if(isFitstShowSuspensionHeadItem){
            if(is(mList.get(firstVisibleItemPosition))){
                return firstVisibleItemPosition;
            }else {
                for(int i = firstVisibleItemPosition; i > -1; i--){
                    if(is(mList.get(i))){
                        return i;
                    }
                }
            }
        }else {
            for(int i = firstVisibleItemPosition; i< mList.size(); i++){
                if(is(mList.get(i))){
                    return i;
                }
            }
        }
        return -1;
    }

    protected abstract int getSuspensionHeadViewResource();

    public abstract void showView(int showSuspensionHeadPosition, View suspensionHeadView);

    public abstract boolean is(T t);
}
